package com.gy.yemall.yemallware.service.impl;

import com.gy.common.utils.R;
import com.gy.yemall.yemallware.entity.vo.SkuHasStock;
import com.gy.yemall.yemallware.fegin.FeignProduct;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.Query;

import com.gy.yemall.yemallware.dao.WareSkuDao;
import com.gy.yemall.yemallware.entity.WareSkuEntity;
import com.gy.yemall.yemallware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private FeignProduct feignProduct;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String wareId = (String) params.get("wareId");
        String skuId = (String) params.get("skuId");

        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 先判读是否存在
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId)
                .eq("ware_id", wareId));
        if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            try {
                R info = feignProduct.info(skuId);
                Map<String, Object> spuInfo = (Map<String, Object>)info.get("spuInfo");
                wareSkuEntity.setSkuName((String) spuInfo.get("skuName"));
                wareSkuDao.insert(wareSkuEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }

    }

    @Override
    public List<SkuHasStock> getSkusHasStock(List<Long> skuIds) {
        // 根据skuIds查出库存
        List<SkuHasStock> collect = skuIds.stream().map((item) -> {
            SkuHasStock skuHasStock = new SkuHasStock();
            Long stock = wareSkuDao.selectBySkuId(item);
            skuHasStock.setSkuId(item);
            // 说明有库存
            skuHasStock.setHasStock(stock==null? false : stock > 0);
            return skuHasStock;
        }).collect(Collectors.toList());

        return collect;
    }

}